home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / MacPerl 5.1.3 / Mac_Perl_513_src / perl5.002 / icemalloc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-26  |  5.6 KB  |  193 lines  |  [TEXT/MPS ]

  1.  
  2. /*
  3. **
  4. ** Notice.
  5. **
  6. ** This source code was written by Tim Endres. time@ice.com
  7. ** Copyright 1988-1991 © By Tim Endres. All rights reserved.
  8. **
  9. ** You may use this source code for any purpose you can dream
  10. ** of as long as this notice is never modified nor removed.
  11. **
  12. */
  13.  
  14. #ifndef __ICEMALLOC__
  15. #define __ICEMALLOC__
  16.  
  17. #include <sys/types.h>
  18.  
  19. #ifdef MALLOC_LOG
  20. void MallocLog(const char * fmt, ...);
  21. #endif
  22.  
  23. /*
  24. ** Right now we are limiting the maximum single allocation unit to 16Meg.
  25. ** This way we can stuff the index to the next ptr hdr into the
  26. ** low 24 bits of a long, then slam 8 bits of flag information
  27. ** into the upper 8 bits of the same long. Not necessarily beautiful
  28. ** but compact and functional.
  29. */
  30.  
  31. /*
  32. **    _PM_DEBUG_LEVEL
  33. **
  34. **  1 - DPRINTF ERROR conditions.
  35. **  2 - DPRINTF *AND* DACTION ERROR conditions.
  36. **
  37. **  3 - DPRINTF WARNING conditions.
  38. **  5 - DPRINTF DEBUGING conditions.
  39. ** 10 - DPRINTF NOTES conditions.
  40. **
  41. */
  42.  
  43. #undef DEBUG
  44.  
  45. #ifdef DEBUG
  46.  
  47. #    define _PM_STATS
  48. #    define _PM_DYNAMIC_MERGING
  49. #    define _PM_DYNAMIC_FREE
  50.  
  51. #    define    _PM_DEBUG_LEVEL        1
  52.  
  53. #    define DPRINTF(level, parms)    { if ((level) <= pool_malloc_debug_level) { printf parms ; } }
  54. #    define DACTION(level, action)    { if ((level) <= pool_malloc_debug_level) { action } }
  55.  
  56. int        pool_malloc_debug_level = _PM_DEBUG_LEVEL;
  57.  
  58. #else
  59.  
  60. #    define _PM_DYNAMIC_MERGING
  61. #    define _PM_DYNAMIC_FREE
  62.  
  63. #    define    _PM_DEBUG_LEVEL        0
  64.  
  65. #    define DPRINTF(level, parms)
  66. #    define DACTION(level, action)
  67.  
  68. #endif DEVELOPMENT
  69.  
  70.  
  71. /*
  72. ** MEMORY PTR HEADER FLAG BITS:
  73. **
  74. ** 01 _PM_PTR_FREE        Is this piece of memory free?
  75. ** 02
  76. ** 04
  77. ** 08
  78. **
  79. ** 10
  80. ** 20
  81. ** 40
  82. ** 80 _PM_PTR_PARITY    This is a parity bit for debugging.
  83. **
  84. */
  85.  
  86. #define _PM_PTR_USED        0x01
  87. #define _PM_PTR_PARITY        0x80
  88.  
  89. #define _PM_MIN_ALLOC_SIZE    8
  90.  
  91. #define    ALIGNMENT                4        /* The 68020 likes things long aligned. */
  92. #define INT_ALIGN(i, r)            ( ((i) + ((r) - 1)) & ~((r) - 1) )
  93.  
  94.  
  95. #define SUGGESTED_BLK_SIZE        32768
  96.  
  97.  
  98. #define GET_PTR_FLAGS(hdr)    \
  99.     ( (u_long) ( (((hdr)->size) >> 24) & 0x000000FF ) )
  100. #define SET_PTR_USED(hdr)    \
  101.     ( (hdr)->size |= (((_PM_PTR_USED) << 24) & 0xFF000000) )
  102. #define SET_PTR_FREE(hdr)    \
  103.     ( (hdr)->size &= ~(((_PM_PTR_USED) << 24) & 0xFF000000) )
  104. #define IS_PTR_USED(hdr)    \
  105.     ( (GET_PTR_FLAGS(hdr) & _PM_PTR_USED) != 0 )
  106. #define IS_PTR_FREE(hdr)    \
  107.     ( (GET_PTR_FLAGS(hdr) & _PM_PTR_USED) == 0 )
  108.  
  109. #define GET_PTR_SIZE(hdr)    \
  110.     ( (u_long) ( ((hdr)->size) & 0x00FFFFFF ) )
  111. #define SET_PTR_SIZE(hdr, blksize)    \
  112.     ( (hdr)->size = ( ((hdr)->size & 0XFF000000) | ((blksize) & 0x00FFFFFF) ) )
  113.  
  114. typedef struct {
  115.     u_long        size;
  116.     } _mem_ptr_hdr, *_mem_ptr_hdr_ptr;
  117.  
  118.  
  119. /* There are two storage methods. Blocks smaller than 64 bytes are allocated
  120.    from a _MEM_BUCKET, larger blocks from a _MEM_BLK.
  121. */
  122.  
  123. typedef struct _MEM_BUCKET {
  124.     struct _MEM_BUCKET * next;                /* Next bucket                                         */
  125.     struct _MEM_BUCKET * prev;                /* Previous bucket                                    */
  126.     struct _MEM_POOL   * pool;                /* The bucket's pool                                    */
  127.     char *                    memory;            /* The bucket's allocated memory.                 */
  128.     char *                    free;                /* First free block                                     */
  129.     short                        max_count;        /* Total # of blocks                                 */
  130.     short                        free_count;        /* # of free blocks in this bucket                 */
  131. } _mem_bucket, *_mem_bucket_ptr;
  132.  
  133. typedef struct _MEM_BLK {
  134.     u_long                size;                /* The size of this block's memory. */
  135.     char                    *memory;            /* The block's allocated memory. */
  136.     u_long                max_free;        /* The maximum free size in the block */
  137.     struct _MEM_BLK    *next;            /* The next block in the pool block list. */
  138.     struct _MEM_POOL    *pool;            /* The block's pool. */
  139.     } _mem_blk, *_mem_blk_ptr;
  140.  
  141.  
  142. typedef struct _MEM_POOL {
  143.     int                    id;                /* The pool's ID.                             */
  144.     u_long                pref_blk_size;    /* The preferred size of new blks.        */
  145.     u_long                limit_blk_size;    /* Maximum size for a single blk.        */
  146.     _mem_bucket_ptr    blk_16;            /* Blocks <= 16 bytes                        */
  147.     _mem_bucket_ptr    free_16;            /* Blocks <= 16 bytes                        */
  148.     _mem_bucket_ptr    blk_32;            /* Blocks <= 32 bytes                        */
  149.     _mem_bucket_ptr    free_32;            /* Blocks <= 32 bytes                        */
  150.     _mem_bucket_ptr    blk_64;            /* Blocks <= 64 bytes                        */
  151.     _mem_bucket_ptr    free_64;            /* Blocks <= 64 bytes                        */
  152.     _mem_blk_ptr        blk_list;        /* The list of blocks in the pool.         */
  153.     struct _MEM_POOL    *next;            /* The next pool in the forest.             */
  154. #ifdef _PM_STATS
  155.     u_long                total_memory;    /* The total allocated memory by this pool */
  156.     u_long                total_storage;    /* The total malloc-able storage in this pool */
  157.     u_long                total_malloc;    /* The total malloc-ed storage not freed. */
  158.     u_long                max_blk_size;    /* The maximum block size allocated. */
  159.     float                    ave_req_size;    /* The ave allocated request size */
  160.     u_long                ave_req_total;    /* The total requests in the average. */
  161.     float                    ave_blk_size;    /* The ave sallocated blk size */
  162.     u_long                ave_blk_total;    /* The total blks in the average. */
  163. #endif
  164.     } _mem_pool, *_mem_pool_ptr;
  165.  
  166.  
  167.  
  168. extern _mem_pool    _mem_system_pool;
  169.  
  170. /*
  171. ** The memory pool forest. To the user, this is simply a disjoint
  172. ** group of memory pools, in which his memory pools lie. We keep
  173. ** it as a simple linked list. Forward linked, nothing fancy.
  174. **
  175. ** The default pool is simply the front pool in the pool forest list.
  176. */
  177. extern _mem_pool_ptr    _mem_pool_forest;
  178.  
  179. #define _default_mem_pool    _mem_pool_forest
  180.  
  181.  
  182. void    *                 pool_malloc(_mem_pool_ptr pool, u_long size);
  183. void    *                 pool_realloc(_mem_pool_ptr pool, void * ptr, u_long size);
  184. int                     pool_free(void * ptr);
  185. int                     free_pool(int id);
  186. int                     free_pool_memory(int id);
  187. _mem_pool_ptr        find_pool(int id);
  188. _mem_pool_ptr        new_malloc_pool(int id, u_long pref_blk_size);
  189. int                     set_default_pool(int id);
  190. int                     merge_free_list();
  191. void                     get_malloc_stats(long * total_memory, long * total_free, long * total_malloc);
  192.  
  193. #endif